package com.javasm.sys.impl;

import com.javasm.sys.tree.TreeUtil;
import com.javasm.sys.MenuService;
import com.javasm.sys.dao.MenuDao;
import com.javasm.sys.SysMenu;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuDao menuDao;

    @Override
    public SysMenu getMenuByid(Integer id) {
        return menuDao.getMenuByid(id);
    }

    @Override
    public List<SysMenu> getallmenu() {
        List<SysMenu> allmenu = menuDao.getallmenu();//获取全部菜单
        List<SysMenu> sysMenus = TreeUtil.buildMenuTree(allmenu, 0);//菜单分级
        return sysMenus;
    }

    @Override
    public List<SysMenu> getMenuByparams(SysMenu sysMenu) {
        List<SysMenu> menuByparams = menuDao.getMenuByparams(sysMenu);
//        List<SysMenu> sysMenus2 = TreeUtil.buildTree(menuByparams, 0);//菜单分级
        return menuByparams;
    }

    @Override
    public List<SysMenu> getMenusbyIds(List<Integer> menuIds) {
        return menuDao.getMenusbyIds(menuIds);
    }

    @Override
    public List<SysMenu> getmenuBytype(Integer menutype) {
        return menuDao.getmenuBytype(menutype);
    }

    @Override
    public Integer getLastIdByParentId(Integer pid) {
        return menuDao.getLastIdByParentId(pid);
    }

    @Override
    public Integer generateId(Integer parentid) {
        String pid="";
        Integer newId=null;//返回的id
        if(parentid == null){
             pid="0";
        }else {
             pid= parentid.toString();//获取父级id
        }

        //查询obj.getPid()下的子部门最大id
        Integer lastIdByParentId = this.menuDao.getLastIdByParentId(Integer.parseInt(pid));

        if(lastIdByParentId==null){
            pid=pid+"01";//如果pid没有子集就新增子集 10+01
            newId= Integer.valueOf(pid);
        }else{
            String newid = String.valueOf(lastIdByParentId+1);//pid有子集,直接新增1
            newId=Integer.parseInt(newid);
            if(newid.endsWith("00"))return null;
        }
        return newId;
    }


    @Override
    public int insert(SysMenu sysMenu) {
        return menuDao.insert(sysMenu);
    }

    @Override
    public int insert2(SysMenu sysMenu) {
        return menuDao.insert2(sysMenu);
    }

    @Override
    public int updateByid(SysMenu sysMenu) {
        return menuDao.updateByid(sysMenu);
    }

    @Override
    public int delById(Integer id) {
        return menuDao.delById(id);
    }
}
